//
//  YYStatusCell.swift
//  YYWeibo
//
//  Created by strongwallyi on 16/1/3.
//  Copyright © 2016年 strongwallyi. All rights reserved.
//  首页微博的 cell 里面包含三个控件

import UIKit
import SnapKit


//cell里面的间距
let YYStatusCellMargin: CGFloat = 8
//头像的宽高
let YYStatusCellIconViewWH: CGFloat = 35


class YYStatusCell: UITableViewCell {
    
    //toolbar 的顶部约束
    var toolBarTopConstraint: Constraint?
    
    //当前视图引用的视图模型(内部有需要的数据)
    var statusViewModel: YYStatusViewModel? {
    
        //设置数据
        didSet{
            statusOriginalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            //更新约束之前先卸载约束
            self.toolBarTopConstraint?.uninstall()
            
            //判断有没有转发微博,有就显示,没有就隐藏
            if statusViewModel?.status?.retweeted_status != nil {
                //有转发微博
                statusRetweetView.hidden = false
                statusRetweetView.statusViewModel = statusViewModel
                //更新约束
                statusToolBar.snp_updateConstraints { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                }

            }else{
                //没有转发微博
                statusRetweetView.hidden = true
                //需要改变底部 toolbar 的约束等于原创微博的底部
                statusToolBar.snp_updateConstraints { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                }
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        selectionStyle = .None
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
        
        //添加视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(statusRetweetView)
        
        //添加约束
        //原创微博的约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView).offset(YYStatusCellMargin)
            make.trailing.equalTo(contentView)
//            make.height.equalTo(80)
        }
        
        //toolbar 的约束
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        //对 contentView 的约束
        contentView.snp_makeConstraints { (make) -> Void in
            //决定了 cell的高度
            make.bottom.equalTo(statusToolBar.snp_bottom)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
        }
        
        //转发微博的约束
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(statusOriginalView)
            make.trailing.equalTo(statusOriginalView)
            make.top.equalTo(statusOriginalView.snp_bottom)
        }
    }
    
    //懒加载控件
    //原创微博
    private lazy var statusOriginalView: YYStatusOriginalView = YYStatusOriginalView()
    
    //toolBar
    private lazy var statusToolBar: YYStatusToolBar = YYStatusToolBar()

    //转发微博
    private lazy var statusRetweetView: YYStatusRetweetView = YYStatusRetweetView()
    
}
